<?php


namespace app\model\member;

use app\model\pay\Huifu;
use think\facade\Cache;
use app\model\BaseModel;

/**
 * 会员开户
 */
class MemberOpenAccount extends BaseModel
{

    /**
     * 添加会员开户
     *
     * @param array $data
     */
    public function addMemberOpenAccount($data)
    {

        $res = model('member_open_account')->add($data);
        Cache::tag("member_open_account")->clear();
        return $this->success($res);
    }

    /**
     * 修改会员标签
     *
     * @param array $data
     * @param array $condition
     */
    public function editMemberOpenAccount($data, $condition)
    {

        $res = model('member_open_account')->update($data, $condition);
        Cache::tag("member_open_account")->clear();
        return $this->success($res);
    }

    /**
     * 删除会员标签
     * @param array $condition
     */
    public function deleteMemberOpenAccount($condition)
    {
        $res = model('member_open_account')->delete($condition);
        Cache::tag("member_open_account")->clear();
        return $this->success($res);
    }


    /**
     * 获取会员标签信息
     *
     * @param array $condition
     * @param string $field
     */
    public function getMemberOpenAccountInfo($condition = [], $field = '*')
    {

        $data = json_encode([ $condition, $field ]);
        $cache = Cache::get("member_open_account_getMemberOpenAccountInfo_" . $data);
        if (!empty($cache)) {
            return $this->success($cache);
        }
        $info = model('member_open_account')->getInfo($condition, $field);
        Cache::tag("member_open_account")->set("member_open_account_getMemberOpenAccountInfo_" . $data, $info);
        return $this->success($info);
    }

    /**
     * 获取会员标签列表
     *
     * @param array $condition
     * @param string $field
     * @param string $order
     * @param string $limit
     */
    public function getMemberOpenAccountList($condition = [], $field = '*', $order = 'create_time desc', $limit = null)
    {

        $data = json_encode([ $condition, $field, $order, $limit ]);
        $cache = Cache::get("member_open_account_getMemberOpenAccountList_" . $data);

        if (!empty($cache)) {
            return $this->success($cache);
        }

        $list = model('member_open_account')->getList($condition, $field, $order, '', '', '', $limit);
        Cache::tag("member_open_account")->set("member_open_account_getMemberOpenAccountList_" . $data, $list);

        return $this->success($list);
    }

    /**
     * 获取会员标签分页列表
     *
     * @param array $condition
     * @param number $page
     * @param string $page_size
     * @param string $order
     * @param string $field
     */
    public function getMemberOpenAccountPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'a.create_time desc', $field = 'a.*',$alias='a',$join=[])
    {
        $list = model('member_open_account')->pageList($condition, $field, $order, $page, $page_size,$alias,$join);
        return $this->success($list);
    }

    public function pass($account_id,$status)
    {
        model('member_open_account')->startTrans();
        try {
            $condition = [['account_id','=',$account_id]];

            $info = model('member_open_account')->getInfo($condition);

            $huifu_model = new Huifu();
            if ($info['account_type']==0){
                $data = [
                    'name'=>$info['realname'],
                    'id_card_num'=>$info['id_card_num'],
                    'id_card_time_type'=>$info['id_card_time_type'],
                    'id_card_start_time'=>$info['id_card_start_time'],
                    'id_card_end_time'=>$info['id_card_end_time'],
                    'mobile'=>$info['mobile'],
                ];

                $result = $huifu_model->personalAccountOpening($data);
            } else {
                $data = [
                    'reg_name'=>$info['enterprise_name'],
                    'license_code'=>$info['tyshxydm'],
                    'license_validity_type'=>$info['shxyzyxq_time_type'],
                    'license_begin_date'=>$info['shxyzyxq_start_time'],
                    'license_end_date'=>$info['shxyzyxq_end_time'],
                    'reg_prov_id'=>$info['province_id'],
                    'reg_area_id'=>$info['city_id'],
                    'reg_district_id'=>$info['district_id'],
                    'reg_detail'=>$info['full_address'].$info['address'],
                    'legal_name'=>$info['realname'],
                    'legal_cert_type'=>'00',
                    'legal_cert_no'=>$info['id_card_num'],
                    'legal_cert_validity_type'=>$info['id_card_time_type'],
                    'legal_cert_begin_date'=>$info['id_card_start_time'],
                    'legal_cert_end_date'=>$info['id_card_end_time'],
                    'contact_name'=>$info['realname'],
                    'contact_mobile'=>$info['mobile'],
                ];

                $result = $huifu_model->businessAccountOpening($data);
            }

            if ($result[ "code" ] < 0)
                return $result;

            $update_data = [
                'status'=>$status,
                'huifu_id'=>$result['data']['huifu_id'],
                'login_name'=>$result['data']['login_name'] ?? '',
                'login_password'=>$result['data']['login_password'] ?? '',
            ];
            $res = model('member_open_account')->update($update_data,$condition);

            model('member_open_account')->commit();

            return $this->success($res);
        } catch (\Exception $e) {
            model('member_open_account')->rollback();
            return $this->error('', $e->getMessage());
        }
    }
}